package com.ruoyi.project.mobile;

import com.ruoyi.common.utils.AesCbcUtil;
import com.ruoyi.common.utils.R;
import com.ruoyi.common.utils.RFactory;
import com.ruoyi.common.utils.WeixinUtils;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.project.system.companyinfo.service.ICompanyInfoService;
import com.ruoyi.project.system.inspectreport.domain.InspectReport;
import com.ruoyi.project.system.inspectreport.service.IInspectReportService;
import com.ruoyi.project.system.member.domain.Member;
import com.ruoyi.project.system.member.service.IMemberService;
import com.ruoyi.project.system.partner.domain.Partner;
import com.ruoyi.project.system.partner.service.IPartnerService;
import com.ruoyi.project.system.relevantimg.service.IRelevantImgService;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 注释
 *
 * @autor LCL
 * @date - 2020/12/9
 */
@RestController
@RequestMapping("/api/info")
public class ApiCompanyInfoController extends BaseController {

    @Autowired
    private ICompanyInfoService companyInfoService;
    @Autowired
    private IPartnerService partnerService;
    @Autowired
    private IRelevantImgService relevantImgService;
    @Autowired
    private IInspectReportService inspectReportService;
    @Autowired
    private IMemberService memberService;

    /**
     * 获取公司信息
     */
    @GetMapping("/getCompanyInfo")
    public R getCompanyInfo(@RequestParam String uid) {
        return RFactory.generateR(this.companyInfoService.findAll());
    }

    /**
     * 查询合作伙伴列表
     * @param uid
     * @return
     */
    @GetMapping("/findPartnerList")
    public R findPartnerList(@RequestParam String uid){
        startPage();
        Partner partner = new Partner();
        partner.setStatus(1);
        return RFactory.generateR(getDataTable(this.partnerService.selectPartnerList(partner)));
    }

    /**
     * 查询合作伙伴详情
     * @param id
     * @return
     */
    @GetMapping("/findPartnerInfo")
    public R findPartnerInfo(@RequestParam String uid,@RequestParam String id){
        Partner partner = partnerService.selectPartnerById(id);
        HashMap<String, Object> map = new HashMap<>();
        //合作伙伴信息
        map.put("partner",partner);
        //图片信息
        map.put("partnerImg",this.relevantImgService.findListByRelIdAndType(id, 0));
        return RFactory.generateR(map);
    }

    /**
     * 查询检验报告列表
     * @param uid
     * @return
     */
    @GetMapping("/findReportList")
    public R findReportList(@RequestParam String uid){
        startPage();
        InspectReport inspectReport = new InspectReport();
        inspectReport.setStatus(1);
        return RFactory.generateR(getDataTable(this.inspectReportService.selectInspectReportList(inspectReport)));
    }

    /**
     * 查询检验报告详情
     * @param id
     * @return
     */
    @GetMapping("/findReportInfo")
    public R findReportInfo(@RequestParam String uid,@RequestParam String id){
        InspectReport inspectReport = inspectReportService.selectInspectReportById(id);
        HashMap<String, Object> map = new HashMap<>();
        //检验报告信息
        map.put("inspectReport",inspectReport);
        //图片信息
        map.put("partnerImg",this.relevantImgService.findListByRelIdAndType(id, 1));
        return RFactory.generateR(map);
    }

    /**
     * 绑定手机号
     * @param map
     * @return
     */
    @PostMapping("/mobile")
    @Transactional
    public R updateMobile(@RequestBody Map<String, Object> map){
        JSONObject webAuthorizeInfo = WeixinUtils.getWebAuthorizeInfo((String) map.get("code"));
        String openid = webAuthorizeInfo.getString("openid");
        // 获取会话密钥（session_key）
        String session_key = webAuthorizeInfo.getString("session_key").replace(" ","+");
        //查询数据库是否存在改用户信息
        Member oldMember = this.memberService.findOneByOpenId(openid);
        if(oldMember==null){
            return R.error("请先授权头像昵称");
        }
        HashMap<String, Object> map1 = new HashMap<>();
        try {
            String decrypts = AesCbcUtil.decrypt(map.get("encryptedData").toString(), session_key,map.get("iv").toString(), "UTF-8");
            if (null != decrypts && decrypts.length() > 0) {

                JSONObject jsons = new JSONObject(decrypts);
                System.out.println("jsons:: "+jsons.toString());
                //更新手机号
                Member member = new Member();
                member.setMobile(jsons.get("purePhoneNumber").toString());
                member.setId(oldMember.getId());
                this.memberService.updateMember(member);
                map1.put("mobile",jsons.get("purePhoneNumber").toString());
            }else{
                return R.error("授权失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return RFactory.generateR(map1);
    }

}
